package com.example.service;


import com.example.dto.QuestionnaireResponse;
import com.example.dto.ReportResponse;
import com.example.entity.DiagnosticRequest;
import com.example.entity.DiagnosticReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

@Service
public class DiagnosticService {

   // @Autowired
    //private DiagnosticMapper diagnosticMapper;

    private final String baseUrl = "http://47.121.193.91:5000/api";

    // 请求问卷
    public QuestionnaireResponse getQuestionnaire(String faceImg, String tongueImg) {
        String url = baseUrl + "/questionnaire";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // 组合请求体
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("face_img", faceImg);
        body.add("tongue_img", tongueImg);

        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(body, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<QuestionnaireResponse> response = restTemplate.exchange(url, HttpMethod.POST, entity, QuestionnaireResponse.class);

        // 保存诊断请求数据
        QuestionnaireResponse responseBody = response.getBody();
        if (responseBody != null) {
            DiagnosticRequest request = new DiagnosticRequest();
            request.setRequestId(responseBody.getRequestId());
            request.setFaceImg(faceImg);
            request.setTongueImg(tongueImg);
            request.setMainCorporeity(responseBody.getMainCorporeity());
            //diagnosticMapper.saveDiagnosticRequest(request);
        }

        return responseBody;
    }

    // 提交问卷
    public void submitQuestionnaire(String requestId, String mainCorporeity, String[] answers) {
        String url = baseUrl + "/corporeityResult";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求体
        String requestBody = "{ \"requestId\": \"" + requestId + "\", \"mainCorporeity\": \"" + mainCorporeity + "\", \"answers\": " + Arrays.toString(answers) + "}";

        HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.exchange(url, HttpMethod.POST, entity, Void.class);
    }

    // 获取诊断报告
    public ReportResponse getReport(String requestId) {
        String url = baseUrl + "/acquireReport";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求体
        String requestBody = "{ \"requestId\": \"" + requestId + "\"}";

        HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ReportResponse> response = restTemplate.exchange(url, HttpMethod.POST, entity, ReportResponse.class);

        // 保存诊断报告数据
        ReportResponse reportResponse = response.getBody();
        if (reportResponse != null) {
            DiagnosticReport report = new DiagnosticReport();
            report.setReportId(reportResponse.getReportId());
            report.setRequestId(requestId);
            report.setMainCorp(reportResponse.getMainCorp());
            report.setReportText(reportResponse.getReportText());
            //.saveDiagnosticReport(report);
        }

        return reportResponse;
    }
}

